#!/usr/bin/env python
# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import dbus
import dbus.service

from swindle import entity


class Service(entity.Entity):
  _I_SERVICE = 'org.chromium.flimflam.Service'

  def __init__(self, bus, dev, profile, name):
    entity.Entity.__init__(self, bus, profile.spath(name))
    self.name = name
    self.dev = dev
    self.profile = profile
    self.profile.add_service(self)

  def die(self):
    self.profile.del_service(self)
    self.remove_from_connection()

  def active(self):
    return self.dev.connected()

  def sname(self):
    return 'Fake'

  @dbus.service.method(_I_SERVICE, in_signature='', out_signature='a{sv}')
  def GetProperties(self):
    self.log('GetProperties')
    r = {}
    r['Type'] = self.dev.technology()
    r['State'] = self.dev.state()
    r['Favorite'] = self.profile.is_favorite()
    r['AutoConnect'] = True
    r['IsActive'] = self.active()
    r['Name'] = self.sname()
    r['Device'] = self.dev.path()
    r['Strength'] = dbus.Byte(self.dev.strength(), variant_level=1)
    return r

  @dbus.service.method(_I_SERVICE, in_signature='sv', out_signature='')
  def SetProperty(self, key, val):
    self.log('SetProperty %s %s', key, val)

  @dbus.service.method(_I_SERVICE, in_signature='s', out_signature='')
  def ClearProperty(self, key):
    self.log('ClearProperty %s', key)

  @dbus.service.method(_I_SERVICE, in_signature='', out_signature='')
  def Connect(self):
    self.log('Connect')
    return self.dev.connect()

  @dbus.service.method(_I_SERVICE, in_signature='', out_signature='')
  def Disconnect(self):
    self.log('Disconnect')
    return self.dev.disconnect()

  @dbus.service.method(_I_SERVICE, in_signature='', out_signature='')
  def Remove(self):
    self.log('Remove')

  @dbus.service.method(_I_SERVICE, in_signature='s', out_signature='')
  def ActivateCellularModem(self, carrier):
    self.log('ActivateCellularModem %s', carrier)
